package qe.spider.grab;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import qe.spider.vo.Plate;
import qe.spider.vo.Stock;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.*;

@Slf4j
@Data
public class ParseThsPlate {
    public static List<String> disks = List.of("D:\\Tools\\THSPlateData\\", "X:\\Tools\\THSPlateData\\", "Y:\\Tools\\THSPlateData\\", "Z:\\Tools\\THSPlateData\\");
    public static final String basePath = System.getProperty("ths_base_path", "D:\\Tools\\") + "THSPlateData\\";

    private File excelFile;
    private Date grabTime;
    private Map<String, Plate> plates = new HashMap<>();

    private List<String> lines = new ArrayList<>();
    private List<String> title = new ArrayList<>();
    private List<Integer> titleIdx = new ArrayList<>();
    private List<Plate> all = new ArrayList<>();

    public ParseThsPlate(File excelFile) throws IOException {
        this.excelFile = excelFile;
        this.grabTime = parseGrabTime();
    }

    public ParseThsPlate(String excelFilePath) throws IOException {
        this.excelFile = new File(excelFilePath);
        this.grabTime = parseGrabTime();
    }

    private Date parseGrabTime() {
        String time = this.excelFile.getName().split("\\.")[0];
        String date = this.excelFile.getParentFile().getName();
        return DateTime.of(date + " " + time, time.contains("_") ? "yyyy-MM-dd HHmmss_SSS" : "yyyy-MM-dd HHmmss");
    }

    public List<String> parseTitle() {
        this.lines = FileUtil.readLines(excelFile, Charset.forName("gb2312"));
        int i = 0;
        for (String t : lines.get(0).split("\t")) {
            title.add(t.trim());
            titleIdx.add(i);
            i++;
        }
        return title;
    }

    public List<Plate> parseAllLines() {
        if (title.isEmpty()) parseTitle();
        for (int i = 1; i < this.lines.size(); i++) {
            List<String> sd = new ArrayList<>();
            for (String t : lines.get(i).split("\t")) {
                sd.add(t.trim());
            }
            if (sd.size() == title.size()) {
                Plate s = parseLinePlate(sd);
                all.add(s);
                plates.put(s.getName(), s);
            }
        }
        return all;
    }

    private Plate parseLinePlate(List<String> items) {
        Plate s = new Plate();
        s.setCode("");
        s.setGrabTime(this.grabTime);
        for (int i = 0; i < title.size(); i++) {
            String t = title.get(i);
            String d = items.get(i);//.replaceAll("-", "");
            if (d.isEmpty()) continue;
            try {
                if ("代码".equals(t)) s.setCode(d);
                else if ("板块名称".equals(t)) {
                    s.setName(d);
//                    if (StrUtil.isEmpty(s.getCode())) s.setCode(d);
                } else if ("涨幅".equals(t) && NumberUtil.isNumber(d.replaceAll("%", "")))
                    s.setZhangFu(new BigDecimal(d.replaceAll("%", "")));
                else if (t.contains("主力金额") && NumberUtil.isNumber(d)) s.setZhuLiJingE(new BigDecimal(d));
                else if (t.contains("主力净量") && NumberUtil.isNumber(d)) s.setZhulijingliang(new BigDecimal(d));
                else if (t.contains("4分钟涨速") && NumberUtil.isNumber(d.replaceAll("%", "")))
                    s.setZhangSu(new BigDecimal(d.replaceAll("%", "")));
//                else if (t.contains("主力买") && NumberUtil.isNumber(d)) s.setZhuLiBuy(new BigDecimal(d));
//                else if (t.contains("主力卖") && NumberUtil.isNumber(d)) s.setZhuLiSell(new BigDecimal(d));
                else if ("流通市值".equals(t) && NumberUtil.isNumber(d)) s.setZongLiuTongZhi(new BigDecimal(d));
                else if ("涨停数".equals(t) && NumberUtil.isNumber(d)) s.setZhangTingCnt(new BigDecimal(d).intValue());
                else if ("涨家数".equals(t) && NumberUtil.isNumber(d)) s.setZhangCnt(new BigDecimal(d).intValue());
                else if ("跌家数".equals(t) && NumberUtil.isNumber(d)) s.setDieCnt(new BigDecimal(d).intValue());
                else if (t.contains("行业指数")) s.setCategory(d.contains("行业指数") ? "行业" : "概念");
            } catch (Exception e) {
                log.error("解析同花顺板块异常----t:{}----d:{}", t, d);
                e.printStackTrace();
            }
        }
        return s;
    }

    public static void main(String[] args) throws IOException {
        ParseThsPlate parser = new ParseThsPlate(new File("D:\\Tools\\THSPlateData\\2024-06-04\\095221.xls"));
        parser.parseTitle();
        parser.parseAllLines();
        log.debug("{}", parser.all);
    }
}
